/**
 * Created by Administrator on 2015/1/25.
 */
var fs = require('fs');
var path = require('path');
var ejs = require('ejs');
var logger = require('./logger');
var config = require('./config');
var dateFormat = require('dateformat');
var xml2js = require('xml2js');
var builder = new xml2js.Builder();  // JSON->xml
var parser = new xml2js.Parser({explicitArray: false, async: false});   //xml -> json

exports.processEjsTemplate = function (templateName, data) {
    var file = path.join(config.Config().templateDir, templateName + ".ejs");
    var ejsStr = fs.readFileSync(file, {encoding: "UTF-8"});
    logger.debug("ejs Template", ejsStr,{debug:true,escape:true});
    return ejs.render(ejsStr, data);
}


exports.getNowTimeStr = function () {
    var now = new Date();
    var timeStr = dateFormat(now, "yyyy-mm-dd HH:MM:ss");
    return timeStr;
}

exports.paraseXML = function(xmlParam){
    var jsonParam = null;
    parser.parseString(xmlParam, function (err, result) {
        jsonParam = result.xml;
    });
    return jsonParam;
}